package com.maozzi.service.blog.biz.consumer;

import com.google.gson.Gson;
import com.maozzi.api.blog.model.BlogDTO;
import com.maozzi.api.mq.handler.MsgHandler;
import com.maozzi.service.blog.constant.BlogConstant;
import com.maozzi.service.blog.domain.BlogDomainService;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 博客 consumer
 *
 * @author maozi
 */
@RocketMQMessageListener(
        consumerGroup = BlogConstant.BLOG_GROUP,
        topic = BlogConstant.BLOG_TOPIC,
        selectorExpression = BlogConstant.BLOG_DEL_TAG
)
@Slf4j
@Component
public class BlogConsumer extends MsgHandler<BlogDTO> implements RocketMQListener<BlogDTO> {
    @Resource
    private BlogDomainService blogDomainService;

    private final static Gson GSON = new Gson();

    @Override
    protected void handleMsg(BlogDTO msg) throws Exception {
        // 模拟
        blogDomainService.del(msg.getId());
    }

    @Override
    protected void handleMaxRetry(BlogDTO msg) throws Exception {
        // 发送hook邮件等
        log.error("消费者收到消息[{}]，超过重试最大次数", GSON.toJson(msg));
    }

    @Override
    protected boolean isRetry() {
        return true;
    }

    @Override
    protected boolean throwException() {
        return false;
    }

    @Override
    public void onMessage(BlogDTO dto) {
        super.dispatchMsg(dto);
    }
}
